package hexapawn.io;

public class UIOAction {
	
	public static enum UIOA_BEHAVIOR {NORMAL, INITIAL_PRESS_ONLY};
	public static enum UIOA_STATE {RELEASED, PRESSED, WAITING_FOR_RELEASE};
	
	private String name;
	private UIOA_BEHAVIOR behavior;
	private int amount;
	private UIOA_STATE state;
	private UIOA_STATE prev_state;
	
	public UIOAction(String name){
		this(name, UIOA_BEHAVIOR.NORMAL);
	}
	
	public UIOAction(String name, UIOA_BEHAVIOR b){
		this.name = name;
		behavior = b;
		reset();
	}
	
	public void reset(){
		prev_state = null;
		state = UIOA_STATE.RELEASED;
		amount = 0;
	}
	
	public String getName(){
		return name;
	}
	
	public synchronized void tap(){
		press();
		release();
	}
	
	public synchronized void press(){
		press(1);
	}
	
	public synchronized void press(int amount){
		if(state != UIOA_STATE.WAITING_FOR_RELEASE){
			this.amount += amount;
			prev_state = state;
			state = UIOA_STATE.PRESSED;
		}
	}
	
	public synchronized void release(){
		prev_state = state;
		state = UIOA_STATE.RELEASED;
	}
	
	public synchronized boolean isPressed(){
		return (getAmount() != 0);
	}
	
	public synchronized boolean isReleased(){
		return(prev_state == UIOA_STATE.PRESSED && state == UIOA_STATE.RELEASED && isPressed());
	}
	
	public synchronized int getAmount(){
		int val = amount;
		if(val != 0)
			if(state == UIOA_STATE.RELEASED)
				amount = 0;
			else if(behavior == UIOA_BEHAVIOR.INITIAL_PRESS_ONLY){
				state = UIOA_STATE.WAITING_FOR_RELEASE;
				amount = 0;
			}
		return val;
	}

}
